package com.ttg.web.dao.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ttg.common.entities.BasePage;
import com.ttg.model.dto.CMReportDTO;
import com.ttg.model.mapper.CostManageMerchDepositLoanMapper;
import com.ttg.model.pojo.CostManageIndustryDepositLoan;
import com.ttg.model.pojo.CostManageMerchDepositLoan;
import com.ttg.model.vo.CMIndustryDepositVO;
import com.ttg.model.vo.CMMerchDepositVO;
import com.ttg.web.dao.CostManageMerchDepositLoanDao;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * Description：
 * Author: chenyou
 * Date: 2025/9/3 11:11
 * Company: ttg
 * Copyright: Copyright (c) 2025
 */
@Service
public class CostManageMerchDepositLoanDaoImpl extends ServiceImpl<CostManageMerchDepositLoanMapper, CostManageMerchDepositLoan> implements CostManageMerchDepositLoanDao {
    @Override
    public CostManageMerchDepositLoan getByUnique(String intervalType,String year,String month, String clsId1, String clsId2, String mctNo) {
        LambdaQueryWrapper<CostManageMerchDepositLoan> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CostManageMerchDepositLoan::getCmmdIntervalType,intervalType)
                .eq(CostManageMerchDepositLoan::getCmmdYear,year)
                .eq(CostManageMerchDepositLoan::getCmmdMonth,month)
                .eq(CostManageMerchDepositLoan::getCmmdClsId1,clsId1)
                .eq(CostManageMerchDepositLoan::getCmmdClsId2,clsId2)
                .eq(CostManageMerchDepositLoan::getCmmdMctNo,mctNo);
        return getOne(wrapper);
    }

    @Override
    public void saveOrUpd(CostManageMerchDepositLoan merchDepositLoan) {
        CostManageMerchDepositLoan old = getByUnique(merchDepositLoan.getCmmdIntervalType(),merchDepositLoan.getCmmdYear(),merchDepositLoan.getCmmdMonth(),merchDepositLoan.getCmmdClsId1(),merchDepositLoan.getCmmdClsId2(),merchDepositLoan.getCmmdMctNo());
        if(Objects.isNull(old)){
            save(merchDepositLoan);
        }else{
            merchDepositLoan.setCmmdId(old.getCmmdId());
            updateById(merchDepositLoan);
        }
    }

    @Override
    public List<String> selectYears() {
        return baseMapper.selectYears();
    }

    @Override
    public List<String> selectMonths(String year) {
        return baseMapper.selectMonths(year);
    }

    @Override
    public BasePage<CMMerchDepositVO> merchDeposit(BasePage page, CMReportDTO reqDto) {
        return baseMapper.merchDeposit(page,reqDto);
    }
}
